package com.smcad.gsm;

import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import android.widget.Toast;

import com.google.android.maps.GeoPoint;

public class Utilities 
{	
	public static void showToast(CharSequence message, Context appContext)
    {
		int duration = Toast.LENGTH_SHORT;

		Toast toast = Toast.makeText(appContext, message, duration);
		toast.show();
    }
	
	static double distance(double fromLat, double fromLon, double toLat, double toLon) {
	    double radius = 6378137;   // approximate Earth radius, *in meters*
	    double deltaLat = toLat - fromLat;
	    double deltaLon = toLon - fromLon;
	    double angle = 2 * Math.asin( Math.sqrt(
	        Math.pow(Math.sin(deltaLat/2), 2) + 
	        Math.cos(fromLat) * Math.cos(toLat) * 
	        Math.pow(Math.sin(deltaLon/2), 2) ) );
	    return radius * angle;
	}
	
	public static GeoPoint getGeopoint(Context ctx, String address) {
		Geocoder coder = new Geocoder(ctx, Locale.getDefault());
		List<Address> addressList;

		try {
			addressList = coder.getFromLocationName(address, 5);
			if (addressList == null) {
				return null;
			}
			Address location = addressList.get(0);
			location.getLatitude();
			location.getLongitude();

			GeoPoint p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
									   (int) (location.getLongitude() * 1E6));

			return p1;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
